const request = require('../../utils/request')

Page({
  data: {
    appointmentList: [],
    loading: false,
    // 状态映射
    statusMap: {
      'PENDING': '待确认',
      'CONFIRMED': '已确认',
      'CANCELLED': '已取消',
      'COMPLETED': '已完成',
      'RESCHEDULED': '已改期'
    },
    // 状态标签颜色
    statusColorMap: {
      'PENDING': '#ffa940',     // 橙色
      'CONFIRMED': '#1890ff',   // 蓝色
      'CANCELLED': '#8c8c8c',   // 灰色
      'COMPLETED': '#52c41a',   // 绿色
      'RESCHEDULED': '#722ed1'  // 紫色
    }
  },

  onLoad() {
    this.getAppointmentList()
  },

  onShow() {
    // 每次显示页面时刷新数据
    this.getAppointmentList()
  },

  // 获取挂号记录列表
  getAppointmentList() {
    this.setData({ loading: true })

    request.get('/student/appointment/getYuYue', {}, true)
      .then(res => {
        this.setData({ loading: false })

        if (res.code === 0 && res.data) {
          console.log('获取挂号记录成功:', res.data)
          // 格式化数据
          const formattedList = res.data.map(item => ({
            ...item,
            formattedTime: this.formatDateTime(item.appointmentTime),
            statusText: this.data.statusMap[item.status] || '未知状态',
            statusColor: this.data.statusColorMap[item.status] || '#8c8c8c'
          }))

          this.setData({
            appointmentList: formattedList
          })
        } else {
          console.error('获取挂号记录失败:', res.message)
          wx.showToast({
            title: res.message || '获取挂号记录失败',
            icon: 'none'
          })
        }
      })
      .catch(err => {
        console.error('获取挂号记录失败:', err)
        this.setData({ loading: false })

        wx.showToast({
          title: '获取挂号记录失败',
          icon: 'none'
        })
      })
  },

  // 取消预约
  cancelAppointment(e) {
    const id = e.currentTarget.dataset.id
    
    wx.showModal({
      title: '提示',
      content: '确定要取消此次预约吗？',
      success: (res) => {
        if (res.confirm) {
          this.setData({ loading: true })
          
          request.post('/student/appointment/cancel', { id }, true)
            .then(res => {
              this.setData({ loading: false })
              
              if (res.code === 0) {
                wx.showToast({
                  title: '取消预约成功',
                  icon: 'success'
                })
                
                // 刷新列表
                this.getAppointmentList()
              } else {
                wx.showToast({
                  title: res.message || '取消预约失败',
                  icon: 'none'
                })
              }
            })
            .catch(err => {
              console.error('取消预约失败:', err)
              this.setData({ loading: false })
              
              wx.showToast({
                title: '取消预约失败',
                icon: 'none'
              })
            })
        }
      }
    })
  },

  // 格式化日期时间
  formatDateTime(dateTimeStr) {
    if (!dateTimeStr) return '';
    
    // 简单处理日期格式化
    const dateTime = new Date(dateTimeStr.replace(/-/g, '/'));
    const year = dateTime.getFullYear();
    const month = (dateTime.getMonth() + 1).toString().padStart(2, '0');
    const day = dateTime.getDate().toString().padStart(2, '0');
    const hours = dateTime.getHours().toString().padStart(2, '0');
    const minutes = dateTime.getMinutes().toString().padStart(2, '0');
    
    return `${year}-${month}-${day} ${hours}:${minutes}`;
  },

  // 下拉刷新
  onPullDownRefresh() {
    this.getAppointmentList();
    wx.stopPullDownRefresh();
  }
}) 